import HttpUtils from '../HttpUtils/HttpUtils';
// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	onLoad(params) {
		//加载栏目导航条
		var folderCode = params.folderCode;
		var currentFolderCode = params.currentFolderCode;
		this.setData({
			"parentId": folderCode,
			"currentFolderCode": currentFolderCode
		});
		this.loadColumnList();
	},
	data: {
		parentId: 0,
		currentFolderCode: 0,
		//分页的几个参数
		pageIndex: 1,
		pageItems: 6,
		totalPages: 1,
		//栏目导航条
		columnList: [],
		//栏目下内容列表
		contentList: [],
		showColumn: true,  //是否展示栏目行	
		showNews: true,		 //是否展示栏目列表
		showData: false,  	//无数据
		noData: "noData",   //noData无数据     noDataHide 展示数据
	},
	loadColumnList: function() {
		//加载中间栏目导航条
		var newUrl = getApp().globalData.dangjianUrl + "/content/relativeFolderQuery";
		var params = {};
		params["pageIndex"] = "1";
		params["pageItems"] = "1000";
		params["code"] = this.data.parentId + '';
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "栏目导航条加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var folders = respData.data.folders.folder;
					if (folders.length == 0) {
						this.loadContentList(this.data.parentId);
						return;
					}
					var firstCode = folders[0].code;
					this.setData({
						"columnList": folders
					});
					this.loadContentList(firstCode);
				} else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},
	loadContentList: function(e) {
		//加载栏目下内容
		var currentFolderCode = this.data.currentFolderCode;
		var folderCode = currentFolderCode;
		if ("object" == typeof (e)) {
			folderCode = e.currentTarget.dataset.code;
		}
		if ("string" == typeof (e)) {
			folderCode = e;
		}
		if (folderCode != currentFolderCode) {
			//设置为第一页
			this.setData({
				"pageIndex": 1,
				"currentFolderCode": folderCode,
				"contentList": []
			});
		}
		var folderCode = this.data.currentFolderCode;
		var newUrl = getApp().globalData.dangjianUrl + "/content/relativeContentQuery";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["code"] = folderCode + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params = JSON.stringify(params);
 		this.httpGet(newUrl, params, "栏目下内容加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					var contents = respData.data.contents;
					var totalPages = contents.totalPages;
					var pageIndex = contents.pageIndex;
					contents = contents.content;
					var contentList = [];
					for (var index in contents) {
						var content = contents[index];
						var json = {};
						var thumbnail = content.thumbnail;
						if (null == thumbnail || "" == thumbnail) {
							var imageFiles = content.imageFiles;
							for (var j in imageFiles) {
								if (imageFiles[j].type == getApp().globalData.imageType) {
									thumbnail = imageFiles[j].url;
									break;
								}
							}
						}
						json["name"] = content.name;
						json["publishTime"] = content.publishTime;
						json["url"] = thumbnail;
						json["code"] = content.code;
						json["type"] = content.type;
						json["count"] = content.count;
						json["fromNa"] = '' == content.fromNa ? "华数": content.fromNa;
						json["folderCode"] = folderCode;
						contentList.push(json);
					}
					var oldContentList = this.data.contentList;
					contentList = oldContentList.concat(contentList);
					var columnList = this.data.columnList;
					for (var index in columnList) {
						var column = columnList[index];
						if (folderCode == column.code) {
							column["className"] = "titOn";
						} else {
							column["className"] = "";
						}
					}
					this.setData({
						"contentList": contentList,
						"columnList": columnList,
						"pageIndex": pageIndex,
						"totalPages": totalPages
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},
	articleDetail(e) {
		var folderCode = e.currentTarget.dataset.foldercode;
		var type = e.currentTarget.dataset.type;
		var code = e.currentTarget.dataset.code;
		var name = e.currentTarget.dataset.name;
		var detailUrl = this.getUrl(type, code, folderCode, name);
		if (detailUrl.length > 0) {
			wx.navigateTo({
				url: detailUrl
			});
		} else {
			this.showPageMsg("tips", "该内容类型详情页暂未开发");
		}
	},

	handleTap1(event) {
		this.setData({
			titOn: event.target.id
		})
	},

	searching(event) {                     //点击搜索
		wx.navigateTo({
			url: '../search/search?key=123'
		})
	},
	//点击键盘完成
	searchSubmit(event) {
		var newUrl = getApp().globalData.dangjianUrl + "/content/contentSearch";
		var params = {};
		params["keyword"] = event.detail.value + "";
		params["siteCode"] = getApp().globalData.siteCode + "";
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["contentType"] = event.currentTarget.dataset.contentType + "";
		params = JSON.stringify(params);
		this.httpGet(newUrl, params, "内容搜索加载失败").then(
			(respData) => {
				var result = respData.data.result.code;
				if (1 == result) {
					if (respData.data.contents.content.length > 0) {
						var contents = respData.data.contents;
						var totalPages = contents.totalPages;
						var pageIndex = contents.pageIndex;
						contents = contents.content;
						var contentList = [];
						var folderCode = this.data.currentFolderCode;
						for (var index in contents) {
							var content = contents[index];
							var json = {};
							var thumbnail = content.thumbnail;
							if (null == thumbnail || "" == thumbnail) {
								var imageFiles = content.imageFiles;
								for (var j in imageFiles) {
									thumbnail = imageFiles[j].url;
									break;
								}
							}
							json["name"] = content.name;
							json["publishTime"] = content.publishTime;
							json["url"] = thumbnail;
							json["code"] = content.code;
							json["type"] = content.type;
							json["count"] = content.count;
							json["fromNa"] = '' == content.fromNa ? "华数" : content.fromNa;
							contentList.push(json);
						}
						this.setData({
							"contentList": contentList,
							// "columnList": columnList,
							"pageIndex": pageIndex,
							"totalPages": totalPages,
							'showColumn': false,
							'showData': false,
							'showNews': true,
						});
					} else {
						this.setData({
							'showColumn': false,
							'showNews': false,
							'showData': true,
						})
					}

				} else {
          this.showPageMsg("alert", respData.data.result.description)				}
			}
		);
 
	},
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex
			});
			this.loadContentList();
		} else {
			this.showPageMsg("tips", "已经是最后一页了");
		}
	},
	onPullDownRefresh: function(e) {
		this.setData({
			pageIndex: 1,
			contentList: []
		});
		this.loadContentList();
		//停止刷新
		wx.stopPullDownRefresh();
	}
});
